﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Xml;
using NUnit.Framework;
using Obra.Api;
using Obra.SmugMug.Api;

namespace Obra.Test
{
    [TestFixture]
    public class SmugMugClientMessageFormatterTester
    {
        [SetUp]
        public void SetUp()
        {
        }

        [TearDown]
        public void TearDown()
        {
        }

        private Message CreateMessageFromStream(
            Stream stream)
        {
            XmlReader reader = XmlReader.Create(
                stream);

            Message message = Message.CreateMessage(
                MessageVersion.Default,
                "action: very carefully",
                reader);

            return message;
        }

        private Message CreateMessageFromFile(
            string filename)
        {
            return this.CreateMessageFromStream(
                new FileStream(filename, FileMode.Open));
        }

        [Test]
        public void EnsureExceptionGenerated()
        {
            Message message = this.CreateMessageFromFile(
                @"..\..\SmugMug\sample\InvalidMethod.xml");

            MessageFormatter formatter =
                new MessageFormatter();

            try
            {
                formatter.DeserializeReply(
                    message,
                    null);
            }
            catch (SmugMugException e)
            {
                Assert.AreEqual("invalid method", e.Message);
            }
            catch (Exception)
            {
                Assert.Fail("Should not be reached.");
            }
        }

        [Test]
        public void ParseChangeAlbumSettingsMessage()
        {
            Message message = this.CreateMessageFromFile(
              @"..\..\SmugMug\sample\SmugMugAlbumsChangeSettings.xml");

            MessageFormatter formatter =
                new MessageFormatter();

            var isNull = formatter.DeserializeReply(
                message,
                null);

            Assert.IsNull(isNull);
        }
    }
}
